package rs.webnet.android.kamere.util;



import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.ByteArrayBuffer;

import rs.webnet.android.kamere.data.Point;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;

public class FileResource {
	private String link = "none";
	private String data = "none";
	private InputStream is;
	private Context context;
	String param = "";

	public FileResource(Context con) {
		this.context = con;
	}

	public FileResource() {
		this.link = "none";
	}

	public void setContext(Context con) {
		this.context = con;
	}

	public void setLink(String link){
		this.link=link;
	}

	public boolean connect() {
		URL url;
		HttpURLConnection conn = null;
		BufferedInputStream bis = null;
		ByteArrayBuffer baf = new ByteArrayBuffer(90);
		try {
			url = new URL(link);
			conn = (HttpURLConnection) url.openConnection();
			conn.connect();
			is = conn.getInputStream();
			bis = new BufferedInputStream(is);
			int current = 0;
			while ((current = bis.read()) != -1) {
				baf.append((byte) current);
			}
			Log.w(">>>>InternetResource>>>>", "uradio connect()");
		} catch (MalformedURLException e) {
			e.printStackTrace();
			Log.w(">>>InternetResource>>err>>>", "MalformedURLException");
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			Log.w(">>>>InternetResource>>err>>1", "IOException");
			return false;
		} catch (IndexOutOfBoundsException e) {
			Log.w("#########internetResource-IndexOutOfBoundsException###########", e);
		} catch (RuntimeException e) {
			e.printStackTrace();
			Log.w(">>>>InternetResource>>err>>", "RuntimeException");
			return false;
		}finally{
			conn.disconnect();
			try {
				bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		data = new String(baf.toByteArray());
		Log.w(">>>>InternetResource>>>>", "connect():" + data);
		
		return true;
	}

	public String getData() {
		Log.w(">>>>InternetResource>>>>", "uradio getData():" + data);
		return data;
	}

	public InputStream getStream() {
		URL myFileUrl = null;
		InputStream is = null;
		HttpURLConnection conn = null;
		try {
			myFileUrl = new URL(link);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			conn = (HttpURLConnection) myFileUrl.openConnection();
			conn.setDoInput(true);
			conn.connect();
			int length = conn.getContentLength();
			is = conn.getInputStream();
		} catch (IOException e) {
			Log.w(">>>>InternetResource>>err>>2", "IOException");
			e.printStackTrace();
		} catch (IndexOutOfBoundsException e) {
			Log.w(">>>>InternetResource>>err>>", "IndexOutOfBoundsException");
			e.printStackTrace();
		} catch (RuntimeException e) {
			Log.w(">>>>InternetResource>>err>>", "RuntimeException");
			e.printStackTrace();
		}finally{
			conn.disconnect();
		}
		Log.w(">>>>InternetResource>>>>", "uradio getStream()");
		return is;
	}

	public void getDataFromStream(InputStream is){
		ByteArrayBuffer baf = new ByteArrayBuffer(90);
		BufferedInputStream bis=null;
		try {
		bis = new BufferedInputStream(is);
		int current = 0;
		while ((current = bis.read()) != -1) {
			baf.append((byte) current);
		}
		Log.w(">>>>InternetResource>>>>", "uradio connect()");
	} catch (MalformedURLException e) {
		e.printStackTrace();
		Log.w(">>>InternetResource>>err>>>", "MalformedURLException");
	} catch (IOException e) {
		e.printStackTrace();
		Log.w(">>>>InternetResource>>err>>1", "IOException");
	} catch (IndexOutOfBoundsException e) {
		Log.w("#########internetResource-IndexOutOfBoundsException###########", e);
	} catch (RuntimeException e) {
		e.printStackTrace();
		Log.w(">>>>InternetResource>>err>>", "RuntimeException");
	}finally{
	
		try {
			bis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	data = new String(baf.toByteArray());
	
	Log.w(">>>>InternetResource>>>>", "connect():" + data);
	
	}
	
	public String getDataFromAssets(String filename){
		try {
			InputStream is = context.getAssets().open(filename);
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String strLine;
			String data = "";
			while ((strLine = br.readLine()) != null) {
				data+=strLine;
			}
			Log.d("d","getDataFromAssets: "+data);	        
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}
}
